import os
from utils import date_util, caiptal_util
from database.mapper import stock_info, funds_info, monitor_result


# 一键诊断
def diagnosis(code):
    # s_info = stock_info.selectone(code)
    # end_date = date_util.get_date_str()  # 结束时间
    # beg_date = date_util.add_day_str(end_date, -60)  # 开始时间
    # dk_list = stock_day_k.select_interval(code, beg_date, end_date)
    # di_list = stock_day_info.select_interval(code, beg_date, end_date)
    # dm_list = stock_day_macd.select_interval(code, end_date, len(dk_list))
    # fund_list = funds_info.select_stock_code(code)
    # board_list = boards_info.select_stock_code(code)

    result_info = {}
    # 近5天,10天,30天,60天资金流,百日
    flow_list = caiptal_util.flow_hundred(code)
    result_info['flow_list_len'] = len(flow_list)
    main_io_sum = 0
    for i in range(len(flow_list)):
        f = flow_list[i]
        main_io_sum += f['main_io']
        if i == 4:
            result_info['five_main_io'] = round(main_io_sum / 10000, 0)
            # print('5个交易日主力资金(万)' + str(round(main_io_sum / 10000, 0)))
        if i == 9:
            result_info['ten_main_io'] = round(main_io_sum / 10000, 0)
            # print('10个交易日主力资金(万)' + str(round(main_io_sum / 10000, 0)))
        if i == 29:
            result_info['thirty_main_io'] = round(main_io_sum / 10000, 0)
            # print('30个交易日主力资金(万)' + str(round(main_io_sum / 10000, 0)))
        if i == 59:
            result_info['sixty_main_io'] = round(main_io_sum / 10000, 0)
            # print('60个交易日主力资金(万)' + str(round(main_io_sum / 10000, 0)))
        if i == len(flow_list) - 1:
            result_info['hundred_main_io'] = round(main_io_sum / 10000, 0)
            # print('100个交易日主力资金(万)' + str(round(main_io_sum / 10000, 0)))

    # 基金资金占比
    s_info = stock_info.selectone(code)
    total_val = s_info.get('total_val')
    cir_val = s_info.get('cir_val')
    fund_list = funds_info.select_stock_code(code)
    result_info['fund_list_len'] = len(fund_list)
    fund_hold = 0
    for fund in fund_list:
        fund_hold += fund['stock_val']
    result_info['total_val'] = total_val
    result_info['cir_val'] = cir_val
    result_info['fund_hold'] = round(fund_hold / 10000, 0)
    result_info['fund_hold_total_prop'] = round(fund_hold / total_val / 100, 2)
    result_info['fund_hold_cir_prop'] = round(fund_hold / cir_val / 100, 2)

    # 机构参与度
    jgcyd_ave = 0
    flow_jgcyd = caiptal_util.flow_jgcyd(code)
    result_info['flow_jgcyd_len'] = len(flow_jgcyd)
    for i in range(len(flow_jgcyd)):
        jgcyd_ave += flow_jgcyd[i]['JGCYD']
        if i == 4:
            result_info['five_jgcyd_ave'] = round(jgcyd_ave / (i + 1) * 100, 2)
        if i == 9:
            result_info['ten_jgcyd_ave'] = round(jgcyd_ave / (i + 1) * 100, 2)
        if i == 14:
            result_info['fifteen_jgcyd_ave'] = round(jgcyd_ave / (i + 1) * 100, 2)
        if i == 19:
            result_info['twenty_jgcyd_ave'] = round(jgcyd_ave / (i + 1) * 100, 2)
        if i == len(flow_jgcyd) - 1:
            result_info['all_jgcyd_ave'] = round(jgcyd_ave / (i + 1) * 100, 2)
    return result_info


# 诊断持久化
def persistence_diagnosis(s_list):
    if len(s_list) == 0:
        s_list = stock_info.selectall()
    for s in s_list:
        if s['total_val'] < 1000:
            continue
        try:
            print('开始诊断' + s['code'])
            diagnosis_info = diagnosis(s['code'])
            monitor_data = {'code': s['code'], 'monitor_date': date_util.get_date_str(), 'monitor_name': '一键诊断',
                            'monitor_type': '3', 'monitor_result': str(diagnosis_info)}
            monitor_result.insert(monitor_data)
        except Exception as e:
            print(str(s) + "诊断持久化异常" + str(e))


if __name__ == '__main__':
    # diagnosis('300014') # 亿纬锂能
    # diagnosis('600031')  # 三一重工
    # diagnosis('600089')  # 特变电工
    # diagnosis('600089')  # 特变电工
    # diagnosis('601888')  # 中国中免
    # diagnosis('000333')  # 美的集团
    # diagnosis('002466')  # 天齐锂业
    # diagnosis('600316')  # 洪都航空
    print(diagnosis('002594'))  # 洪都航空
    # persistence_diagnosis([{'code': '003009', 'total_val': 100}])
    print("end")
